// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SA1401FieldsMustBePrivateBulbItem.cs" company="StyleCop for ReSharper Development Team">
//   Copyright (c) StyleCop for ReSharper Development Team. All rights reserved.
// </copyright>
// <summary>
//   Defines the SA1401FieldsMustBePrivateBulbItem type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace StyleCop.For.ReSharper.BulbItems.Maintainability
{
    #region Using Directives

    using JetBrains.ProjectModel;
    using JetBrains.ReSharper.Psi;
    using JetBrains.ReSharper.Psi.CSharp.Impl;
    using JetBrains.ReSharper.Psi.CSharp.Tree;
    using JetBrains.ReSharper.Psi.Tree;
    using JetBrains.TextControl;

    using StyleCop.For.ReSharper.BulbItems.Framework;

    #endregion

    /// <summary>
    /// BulbItem -SA1400AccessModifierMustBeDeclaredBulbItem : Inserts a modifier.
    /// </summary>
    internal class SA1401FieldsMustBePrivateBulbItem : TransactionalBulbItem
    {
        #region Public Methods

        /// <summary>
        /// Performs the QuickFix, changes the modifier to private.
        /// </summary>
        /// <param name="solution">
        /// Current Solution.
        /// </param>
        /// <param name="textControl">
        /// Current Text Control to modify.
        /// </param>
        public override void ExecuteInner(ISolution solution, ITextControl textControl)
        {
            var element = Utils.GetElementAtCaret(solution, textControl);

            var containingElement = (IElement)element.GetContainingElement<IFieldDeclarationNode>(true) ?? element.GetContainingElement<IMultipleDeclarationNode>(true);

            if (containingElement == null)
            {
                var treeNode = (ITreeNode)element;

                containingElement = treeNode.PrevSibling;
            }

            ModifiersUtil.SetAccessRights(containingElement, AccessRights.PRIVATE);
        }

        #endregion
    }
}